#!/bin/sh

set -e

UPDATENOTIFIERDIR=/var/lib/update-notifier/user.d

case "$1" in
    configure)
	unset DISPLAY # No GUI launched from postinst please
	caja-dropbox update || true

        echo "Please restart all running instances of Caja, or you will experience problems. i.e. caja --quit"

        if [ -d $UPDATENOTIFIERDIR ] ; then
          # pgrep matches application names from /proc/<pid>/status which is
          # truncated according to sys/procfs.h definition. Problem is it's
          # platform dependent. Either 15 or 16 chars.
          if pgrep -x caja > /dev/null 2>&1 ;  then
            cat > $UPDATENOTIFIERDIR/dropbox-restart-required <<DROPBOXEND
Name: Caja Restart Required
Priority: High
Terminal: False
Command: caja -q
ButtonText: _Restart Caja
DontShowAfterReboot: True
DisplayIf: pgrep -x caja -U \$(id -u) > /dev/null
Description: Dropbox requires Caja to be restarted to function properly.
DROPBOXEND
          else
            rm -f $UPDATENOTIFIERDIR/dropbox-restart-required
          fi
        fi

        echo 'Dropbox installation successfully completed! You can start Dropbox from your applications menu.'

        if [ "$2" = "" ] && [ -d $UPDATENOTIFIERDIR ] ; then
            cat > $UPDATENOTIFIERDIR/dropbox-start-required <<DROPBOXEND
Name: Dropbox Start Required
Priority: High
Command: caja-dropbox start -i
Terminal: False
DontShowAfterReboot: True
DisplayIf: dropbox running > /dev/null
Description: Start Dropbox to finish installation.
ButtonText: _Start Dropbox
DROPBOXEND
        fi

	;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
